# -*- coding: utf-8 -*-
# @Time : 2018/12/21 10:15
# @Author : Administrator
# @File : 001.py
# @Project : python_demo
# @title : 基础脚本
import unittest

import time

import os,HTMLTestRunner
from selenium import webdriver
from py41.test_unit.csv_read import Data_Read
from py41.test_unit import agileoneV1, agileone


class TestAgileonelogin(unittest.TestCase):
    def setUp(self):
        self.driver=webdriver.Firefox()
        self.driver.get("http://localhost:8088/agileone")
        self.data=Data_Read()#实例化Data_Read(),方便后面使用里面的方法
        self.list=self.data.read_csv("data.csv")   #调用Data_Read()类中的方法
        # print(self.list)
    def test_login001(self):
        username=self.list[0]["username"]
        password = self.list[0]["password"]
        self.driver.find_element_by_id("username").send_keys(username)
        self.driver.find_element_by_id("password").send_keys(password)
        self.driver.find_element_by_id("login").click()
        time.sleep(3)
        act=self.driver.find_element_by_partial_link_text("注销").text
        expect="注销"
        self.assertEqual(act,expect)#断言登录是否成功

    def test_login002(self):
        username=self.list[1]["username"]
        password = self.list[1]["password"]
        self.driver.find_element_by_id("username").send_keys(username)
        self.driver.find_element_by_id("password").send_keys(password)
        self.driver.find_element_by_id("login").click()
        time.sleep(3)
        act=self.driver.find_element_by_id("msg").text
        expect="出错啦: 用户名不能为空"
        self.assertIn(expect,act)#断言用户名为空时，登录失败
    def test_login003(self):
        username=self.list[2]["username"]
        password = self.list[2]["password"]
        self.driver.find_element_by_id("username").send_keys(username)
        self.driver.find_element_by_id("password").send_keys(password)
        self.driver.find_element_by_id("login").click()
        time.sleep(3)
        act = self.driver.find_element_by_id("msg").text
        expect="出错啦: 密码输入错误"
        self.assertIn(expect, act)  # 断言密码错误时，登录失败
    def tearDown(self):
        self.driver.quit()
class TestAgileoneadd(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Firefox()
        cls.driver.get("http://localhost:8088/agileone")
        cls.driver.find_element_by_id("username").send_keys("admin")
        cls.driver.find_element_by_id("password").send_keys("admin")
        cls.driver.find_element_by_id("login").click()
        time.sleep(3)

    def test_001_skip(self):
        self.driver.find_element_by_partial_link_text("公告管理").click()
        time.sleep(3)

    def test_002_add(self):
        self.driver.find_element_by_id("headline").send_keys("圣诞快乐")
        self.driver.find_element_by_css_selector("img.ke-common-icon.ke-icon-source").click()
        self.driver.find_element_by_class_name("ke-textarea").send_keys("Everybody~Merry Christmas!")
        self.driver.find_element_by_id("add").click()
        time.sleep(3)
        act=self.driver.find_element_by_id("msg").text
        expect="成功啦: 新增数据成功"
        self.assertIn(expect,act)
    def test_003_edit(self):

        self.driver.find_element_by_xpath("/html/body/table/tbody/tr[2]/td[2]/div/div[2]"
                                          "/table[2]/tbody/tr[1]/td[5]/label[1]").click()
        time.sleep(3)
        self.driver.find_element_by_id("headline").clear()
        time.sleep(3)
        self.driver.find_element_by_id("headline").send_keys("新年快乐")
        self.driver.find_element_by_class_name("ke-textarea").clear()
        time.sleep(3)
        self.driver.find_element_by_class_name("ke-textarea").send_keys("Everybody~Happy NewYear!")
        self.driver.find_element_by_id("edit").click()
        time.sleep(3)
        act = self.driver.find_element_by_id("msg").text
        expect = "成功啦: 更新数据成功"
        self.assertIn(expect, act)
    @classmethod
    def tearDownClass(cls):
        os.system("taskkill /F /IM Firefox.exe")

class TestAgileoneadd_cust(unittest.TestCase):
    @classmethod
    def get_driver(cls):
        cls.driver = webdriver.Firefox()
        cls.driver.get("http://localhost:8088/agileone")
        cls.driver.find_element_by_id("username").send_keys("admin")
        cls.driver.find_element_by_id("password").send_keys("admin")
        cls.driver.find_element_by_id("login").click()
        time.sleep(3)

        return cls.driver

    def test_001_skip(self):
        T = TestAgileoneadd_cust()
        self.driver = T.get_driver()
        self.driver.find_element_by_partial_link_text("公告管理").click()
        time.sleep(3)

    def test_002_add(self):
        self.driver.find_element_by_id("headline").send_keys("圣诞快乐")
        self.driver.find_element_by_css_selector("img.ke-common-icon.ke-icon-source").click()
        self.driver.find_element_by_class_name("ke-textarea").send_keys("Everybody~Merry Christmas!")
        self.driver.find_element_by_id("add").click()
        time.sleep(3)
        act = self.driver.find_element_by_id("msg").text
        expect = "成功啦: 新增数据成功"
        self.assertIn(expect, act)

    def test_003_edit(self):
        self.driver.find_element_by_xpath("/html/body/table/tbody/tr[2]/td[2]/div/div[2]"
                                          "/table[2]/tbody/tr[1]/td[5]/label[1]").click()
        time.sleep(3)
        self.driver.find_element_by_id("headline").clear()
        time.sleep(3)
        self.driver.find_element_by_id("headline").send_keys("新年快乐")
        self.driver.find_element_by_class_name("ke-textarea").clear()
        time.sleep(3)
        self.driver.find_element_by_class_name("ke-textarea").send_keys("Everybody~Happy NewYear!")
        self.driver.find_element_by_id("edit").click()
        time.sleep(3)
        act = self.driver.find_element_by_id("msg").text
        expect = "成功啦: 更新数据成功"
        self.assertIn(expect, act)

    @classmethod
    def close_driver(cls):
        cls.driver.quit()

if __name__ == '__main__':
    # unittest.main()
    suite=unittest.TestSuite()
    suite.addTests(unittest.TestLoader().loadTestsFromModule(agileoneV1))

    # suite.addTests(unittest.TestLoader().loadTestsFromModule(agileone))
    with open("AgileoneTestReport.html",'w',encoding="utf-8") as f:
        runner=HTMLTestRunner.HTMLTestRunner(stream=f,verbosity=2,title="测试报告",description="Agileone测试登录-新增公告-编辑公告")
        runner.run(suite)